<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  <title>lvmx-express</title>
  <link rel="stylesheet" href="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/css/bootstrap.min.css" />
  <link rel="stylesheet" href="https://cdn.bootcss.com/font-awesome/5.11.2/css/all.min.css">
  <link rel="stylesheet" href="/stylesheets/style.css">
</head>

<body>
  <nav class="navbar navbar-dark bg-info navbar-expand-lg">
    <a class="navbar-brand" href="/">
      <img src="https://v4.bootcss.com/docs/4.3/assets/brand/bootstrap-solid.svg" width="30" height="30"
        class="d-inline-block align-top" alt="" />
      Bootstrap
    </a>

    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent">
      <span class="navbar-toggler-icon"></span>
    </button>

    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav mr-auto">
        <li class="nav-item active">
          <a class="nav-link" href="/posts">文章列表</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="/posts/create">文章新增</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="/chat">聊天室</a>
        </li>
      </ul>

      <ul class="navbar-nav">
        <li class="nav-item">
          <a class="nav-link" href="/users/login">Login</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="/users/register">Register</a>
        </li>
      </ul>
    </div>
  </nav>

  <div class="container my-5">
    <div class="row">
      <div class="col-md-6 mx-auto">
        <h1 class="text-center text-uppercase py-5">
          <span style="letter-spacing: 12px; font-weight: 100;">
            Web<span style="font-weight: 300;">socket</span>
          </span>
        </h1>

        <div id="messages" class="messages mb-4" style="height: 55vh; overflow: scroll;">
          <!-- <% for (let i = 0; i < 10; i++) { %>
          <div class="message my-4 d-flex">
            <div class="mr-2">
              <small class="text-black-50">System: </small>
            </div>
            <div>
              Hello.
            </div>
          </div>
          <% } %> -->
        </div>
      </div>
    </div>
    <div class="row">
      <div class="col-md-6 mx-auto mb-4 fixed-bottom">
        <input id="message" type="text" class="form-control">
      </div>
    </div>
  </div>

  <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
  <script src="/javascripts/socket.io.js"></script>
  <script>
    const socket = io()

    socket.on('connect', () => {
      console.log('连接成功')
      socket.emit('setUsername', "<%= username %>")
    })

    $('#message').keyup(function (event) {
      if (event.keyCode === 13) {
        event.preventDefault()
        const message = $(this).val()
        $(this).val('')

        socket.emit('message', message)
      }
    })

    socket.on('message', (data) => {
      console.log('接受了来自服务器的消息')
      $('#messages').append(`
        <div class="message my-4 d-flex">
          <div class="mr-2">
            <small class="text-black-50">${data.username}: </small>
          </div>
          <div>
            ${data.message}
          </div>
        </div>
      `)

      $('#messages').stop().animate({ scrollTop: $('#messages').height() + $('#messages').scrollTop() }, 'slow')
    })

  </script>
</body>

</html>